/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.loveModelRecord.service;

import com.fzhucc.modules.loveModelRecord.domain.LoveModelRecord;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.loveModelRecord.repository.LoveModelRecordRepository;
import com.fzhucc.modules.loveModelRecord.domain.dto.LoveModelRecordDto;
import com.fzhucc.modules.loveModelRecord.domain.dto.LoveModelRecordQueryCriteria;
import com.fzhucc.modules.loveModelRecord.service.mapstruct.LoveModelRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-06-12
**/
@Service
@RequiredArgsConstructor
public class LoveModelRecordService {

    private final LoveModelRecordRepository loveModelRecordRepository;
    private final LoveModelRecordMapper loveModelRecordMapper;

    public Map<String,Object> queryAll(LoveModelRecordQueryCriteria criteria, Pageable pageable){
        Page<LoveModelRecord> page = loveModelRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(loveModelRecordMapper::toDto));
    }

    public List<LoveModelRecordDto> queryAll(LoveModelRecordQueryCriteria criteria){
        return loveModelRecordMapper.toDto(loveModelRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public LoveModelRecordDto findById(Integer id) {
        LoveModelRecord loveModelRecord = loveModelRecordRepository.findById(id).orElseGet(LoveModelRecord::new);
        ValidationUtil.isNull(loveModelRecord.getId(),"LoveModelRecord","id",id);
        return loveModelRecordMapper.toDto(loveModelRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    public LoveModelRecordDto create(LoveModelRecord resources) {
        return loveModelRecordMapper.toDto(loveModelRecordRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(LoveModelRecord resources) {
        LoveModelRecord loveModelRecord = loveModelRecordRepository.findById(resources.getId()).orElseGet(LoveModelRecord::new);
        ValidationUtil.isNull( loveModelRecord.getId(),"LoveModelRecord","id",resources.getId());
        loveModelRecord.copy(resources);
        loveModelRecordRepository.save(loveModelRecord);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            loveModelRecordRepository.deleteById(id);
        }
    }

    public void download(List<LoveModelRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LoveModelRecordDto loveModelRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", loveModelRecord.getUserId());
            map.put("劳模id", loveModelRecord.getModelId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}